﻿var loginNameValue;

function AfterDataValidation(response)
{
	$('.principal').hide();
	$('.instruction').hide();
	$('.filter-option').hide();
	$('.Validation').show();
}

function validateApplications()
{
	var applications = [];
	var rang = 1;
	if ($('#sortable2').children('li.grab').length > 0)
	{
		$('#sortable2').children('li.grab').each(function ()
		{
			var oblig = false;
			applications.push({
				value: loginNameValue,
				id: $(this).attr('id'),
				rang: rang,
				obligatoire: oblig
			});
			rang++;
		});
		callApplicationPageByUser({ keyApplication: applications }, AfterDataValidation);
	}
	else
	{
		//Remove all applications
		callApplicationPageByUser({ loginName: loginNameValue }, AfterDataValidation);
	}
}
function setSelectedApplications(response)
{
	if (response.length === 0)
	{
		$('#sortable2').children('li.emptyMessage').show();
	}
	else
	{
		$('#sortable2').children('li.emptyMessage').hide();
		for (var i = 0; i < response.length; i++)
		{
			var newItem = $('<li id="' + response[i].Id + '" class="perso grab manager-selection"><div>' + response[i].Name + '</div></li>');
			var newPosition = $('<div><span>' + (i + 1) + '</span></div>');
			if (response[i].Description !== null && response[i].Description !== "")
			{
				newItem.attr('description', response[i].Description);
				newItem.bind('mouseover', function ()
				{
					var desc = $(this).attr('description');
					$(this).append("<div class='info' style='position:absolute;top:0'>" + response[i].Name + "</div>");
				});
				newItem.bind('mouseout', function ()
				{
					$(this).children('div.info').remove();
				});
			}
			newItem.appendTo('#sortable2');
			newPosition.appendTo('div.positions');
		}
	}
}

$(function ()
{
	var directionName;
	if ($("input#maDirection").val() !== null)
	{
		directionName = $('input#maDirection').val();
	}
	initTeamsFilters(directionName);
	getFilterValues();
	//	getAvailableApplications();

	if ($("input#userLogin").val() !== null)
	{
		loginNameValue = $('input#userLogin').val();
	}
	var dataParameters = { search: 'application',
		loginName: loginNameValue,
		filterType: userFilterType,
		filterValue: userFilterValue
	};
	callApplicationPageByUser(dataParameters, setSelectedApplications);

	var dataParametersDirectionApplications = { search: 'application',
		filterType: 'directionRecursive',
		filterValue: directionName
	};
	callApplicationPageSelectedApps(dataParametersDirectionApplications, setAvailableApplications);
});
